/*
 * =====================================================================================
 *
 *       Filename:  cpu_tool_jni.c
 *
 *    Description:  cpu_tools's interfaces for java
 *
 *        Version:  1.0
 *        Created:  2013年11月16日 12时42分32秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  BeeSui Xue (BX), xuejilong@foxmail.com
 *        Website:  http://beesui.com
 *        License:  GNU General Public License
 *   Organization:  Free Person
 *
 * =====================================================================================
 */

#include "CpuTool.h"
#include<string.h>


/*
 * Class:     CpuTool
 * Method:    getScalingCurFreq
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getScalingCurFreq
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	return get_scaling_cur_freq(cpuNum);
}

/*
 * Class:     CpuTool
 * Method:    getScalingMinFreq
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getScalingMinFreq
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	return get_scaling_min_freq(cpuNum);
}

/*
 * Class:     CpuTool
 * Method:    getScalingMaxFreq
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getScalingMaxFreq
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	return get_scaling_max_freq(cpuNum);
}

/*
 * Class:     CpuTool
 * Method:    getScalingAvailableFrequences
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_CpuTool_getScalingAvailableFrequences
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	char result[1024];
	get_scaling_available_frequences(result, cpuNum);
	return (*env)->NewStringUTF(env, result);
}

/*
 * Class:     CpuTool
 * Method:    getScalingAvailableGovernors
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_CpuTool_getScalingAvailableGovernors
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	char result[1024];
	get_scaling_available_governors(result, cpuNum);
	return (*env)->NewStringUTF(env, result);
}

/*
 * Class:     CpuTool
 * Method:    getScalingGovernor
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_CpuTool_getScalingGovernor
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	char result[1024];
	get_scaling_governor(result, cpuNum);
	return (*env)->NewStringUTF(env, result);
}

/*
 * Class:     CpuTool
 * Method:    getScalingSetSpeed
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getScalingSetSpeed
  (JNIEnv *env, jobject obj, jint cpuNum)
{
	return get_scaling_setspeed(cpuNum);
}

/*
 * Class:     CpuTool
 * Method:    setCpuGovernor
 * Signature: (Ljava/lang/String;I)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_setCpuGovernor
  (JNIEnv *env, jobject obj, jstring string, jint cpuNum)
{
	const char *str = (*env)->GetStringUTFChars(env, string, 0);
	char result[1024];
	strcpy(result, str);
	(*env)->ReleaseStringUTFChars(env, string, str);
	return set_cpu_governor(result, cpuNum);
}

/*
 * Class:     CpuTool
 * Method:    setScalingSetSpeed
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_setScalingSetSpeed
  (JNIEnv *env, jobject obj, jint freq, jint cpuNum)
{
	return set_scaling_setspeed(freq, cpuNum);
}

/*
 * Class:     CpuTool
 * Method:    getCpuUsage
 * Signature: (IILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getCpuUsage
  (JNIEnv *env, jobject obj, jint step, jint total_steps, jstring string)
{
	const char *str = (*env)->GetStringUTFChars(env, string, 0);
	char filename[1024];
	strcpy(filename, str);
	(*env)->ReleaseStringUTFChars(env, string, str);
	return get_cpu_usage(step, total_steps, filename);
}

/*
 * Class:     CpuTool
 * Method:    getNetworkUsage
 * Signature: (IILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getNetworkUsage
  (JNIEnv *env, jobject obj, jint step, jint total_steps, jstring string)
{
	const char *str = (*env)->GetStringUTFChars(env, string, 0);
	char filename[1024];
	strcpy(filename, str);
	(*env)->ReleaseStringUTFChars(env, string, str);
	return get_network_usage(step, total_steps, filename);
}

/*
 * Class:     CpuTool
 * Method:    getMemoryUsage
 * Signature: (IILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getMemoryUsage
  (JNIEnv *env, jobject obj, jint step, jint total_steps, jstring string)
{
	const char *str = (*env)->GetStringUTFChars(env, string, 0);
	char filename[1024];
	strcpy(filename, str);
	(*env)->ReleaseStringUTFChars(env, string, str);
	return get_memory_usage(step, total_steps, filename);
}

/*
 * Class:     CpuTool
 * Method:    getDiskUsage
 * Signature: (IILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_getDiskUsage
  (JNIEnv *env, jobject obj, jint step, jint total_steps, jstring string)
{
	const char *str = (*env)->GetStringUTFChars(env, string, 0);
	char filename[1024];
	strcpy(filename, str);
	(*env)->ReleaseStringUTFChars(env, string, str);
	return get_disk_usage(step, total_steps, filename);
}

/*
 * Class:     CpuTool
 * Method:    genJPGFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_genJPGFile
  (JNIEnv *env, jobject obj, jstring input_s, jstring output_s)
{
	const char *in = (*env)->GetStringUTFChars(env, input_s, 0);
	const char *out = (*env)->GetStringUTFChars(env, output_s, 0);
	char input_file[1024];
	char output_file[1024];
	strcpy(input_file, in);
	strcpy(output_file, out);
	(*env)->ReleaseStringUTFChars(env, input_s, in);
	(*env)->ReleaseStringUTFChars(env, output_s, out);
	return gen_jpg_file(input_file, output_file);
}

/*
 * Class:     CpuTool
 * Method:    genPDFFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_genPDFFile
  (JNIEnv *env, jobject obj, jstring input_s, jstring output_s)
{
	const char *in = (*env)->GetStringUTFChars(env, input_s, 0);
	const char *out = (*env)->GetStringUTFChars(env, output_s, 0);
	char input_file[1024];
	char output_file[1024];
	strcpy(input_file, in);
	strcpy(output_file, out);
	(*env)->ReleaseStringUTFChars(env, input_s, in);
	(*env)->ReleaseStringUTFChars(env, output_s, out);
	return gen_pdf_file(input_file, output_file);
}

/*
 * Class:     CpuTool
 * Method:    genPNGFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_genPNGFile
  (JNIEnv *env, jobject obj, jstring input_s, jstring output_s)
{
	const char *in = (*env)->GetStringUTFChars(env, input_s, 0);
	const char *out = (*env)->GetStringUTFChars(env, output_s, 0);
	char input_file[1024];
	char output_file[1024];
	strcpy(input_file, in);
	strcpy(output_file, out);
	(*env)->ReleaseStringUTFChars(env, input_s, in);
	(*env)->ReleaseStringUTFChars(env, output_s, out);
	return gen_png_file(input_file, output_file);
}

/*
 * Class:     CpuTool
 * Method:    genCSVFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_CpuTool_genCSVFile
  (JNIEnv *env, jobject obj, jstring input_s, jstring output_s)
{
	const char *in = (*env)->GetStringUTFChars(env, input_s, 0);
	const char *out = (*env)->GetStringUTFChars(env, output_s, 0);
	char input_file[1024];
	char output_file[1024];
	strcpy(input_file, in);
	strcpy(output_file, out);
	(*env)->ReleaseStringUTFChars(env, input_s, in);
	(*env)->ReleaseStringUTFChars(env, output_s, out);
	return gen_csv_file(input_file, output_file);
}
